﻿using System;

namespace Tools
{
    /// <summary>
    /// 常用类型转换扩展方法
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// 将对象安全地转换为int类型，失败返回默认值  四舍五入
        /// </summary>
        public static int ToInt(this object obj, int defaultValue = 0)
        {
            if (obj == null)
                return defaultValue;

            if (obj is int i)
                return i;

            if (int.TryParse(obj.ToString(), out int result))
                return result;

            if (double.TryParse(obj.ToString(), out double d))
                return Convert.ToInt32(d);

            return defaultValue;
        }

        /// <summary>
        /// 将对象安全地转换为long类型，失败返回默认值 四舍五入
        /// </summary>
        public static long ToLong(this object obj, long defaultValue = 0)
        {
            if (obj == null)
                return defaultValue;

            if (obj is long l)
                return l;

            if (long.TryParse(obj.ToString(), out long result))
                return result;

            if (double.TryParse(obj.ToString(), out double d))
                return Convert.ToInt64(d);

            return defaultValue;
        }

        /// <summary>
        /// 将对象安全地转换为double类型，失败返回默认值
        /// </summary>
        public static double ToDouble(this object obj, double defaultValue = 0d)
        {
            if (obj == null)
                return defaultValue;

            if (obj is double d)
                return d;

            if (double.TryParse(obj.ToString(), out double result))
                return result;

            return defaultValue;
        }

        /// <summary>
        /// 将对象安全地转换为decimal类型，失败返回默认值
        /// </summary>
        public static decimal ToDecimal(this object obj, decimal defaultValue = 0m)
        {
            if (obj == null)
                return defaultValue;

            if (obj is decimal dec)
                return dec;

            if (decimal.TryParse(obj.ToString(), out decimal result))
                return result;

            return defaultValue;
        }

        /// <summary>
        /// 将对象安全地转换为bool类型，失败返回默认值
        /// </summary>
        public static bool ToBool(this object? obj, bool defaultValue = false)
        {
            if (obj == null)
                return defaultValue;

            if (obj is bool b)
                return b;

            if (bool.TryParse(obj.ToString(), out bool result))
                return result;

            // 额外支持常见1/0、yes/no、true/false字符串
            var str = obj.ToString().Trim().ToLower();
            if (str == "1" || str == "yes" || str == "true")
                return true;
            if (str == "0" || str == "no" || str == "false")
                return false;

            return defaultValue;
        }

        /// <summary>
        /// 将对象安全地转换为字符串，null返回空字符串
        /// </summary>
        public static string ToSafeString(this object obj)
        {
            return obj?.ToString() ?? string.Empty;
        }
    }
}
